$(function(){
	$('#tt').datagrid({
		url:'/simuatorStatusInfo/simuatorStatusInfoPage'
	});
	$("#modelByType").combobox({
		onSelect:function(){
		    var value = $('#modelByType').combobox('getValue');
		   	$('#upgradeSysApkByType').datagrid({
				url:'/systemapk/configList?json&isDelete=0&type=' + value
			});
		}
    });
});

function doSearch(){
	var status=$('#statusId').combobox('getValue');
	var taskStatus=$('#taskStatusId').combobox('getValue');
	var modelId = $('#modelId').combobox('getValue');
	var bizVerId=$('#bizVerId').val();
	var sysVerId=$('#sysVerId').val();
	var heartVerId = $('#heartVerId').val();
	var uuid=$('#suuidId').val();
	var clientIp=$('#sclientIpId').val();
	var mac=$('#macId').val();
	$('#tt').datagrid({
		url:'/simuatorStatusInfo/simuatorStatusInfoPage',
		queryParams:{
			status:status,
			taskStatus:taskStatus,
			model : modelId,
			bizVer:bizVerId,
			heartVer:heartVerId,
			sysVer:sysVerId,
			uuid:uuid,
			clientIp:clientIp,
			mac:mac
		}
	});
	$('#tt').datagrid('unselectAll');
}

function onLineFormatter(value){
	if(value==1){
		return '<span style="color:green">在线</span>';
	}else{
		return '<span style="color:red">离线</span>';
	}
}

function taskTypeFormatter(value){
	if(value=='upgrade'){
		return '<span style="color:green">升级</span>';
	}else if(value=='close'){
		return '<span style="color:red">关闭手机</span>';
	}else if(value=='restart') {
		return '<span style="color:red">重启手机</span>';
	} else if(value=='openFlashlight') {
		return '<span style="color:red">打开手电</span>';
	} else if (value=='upgradeHert') {
		return '<span style="color:green">升级心跳包</span>';
	} else if (value=='upgradeSysApk') {
		return '<span style="color:green">升级系统包</span>';
	}else {
		return '<span style="color:red">无任务</span>';
	}
	
}

function taskStatuFormatter(value){
	if(value==0){
		return '<span style="color:green">空闲</span>';
	}else if(value==1){
		return '<span style="color:red">下发任务</span>';
	}else if(value==2){
		return '<span style="color:red">接受任务</span>';
	}else if(value==3){
		return '<span style="color:red">任务超时</span>';
	}else if(value==4){
		return '<span style="color:red">任务完成</span>';
	}else{
		return '<span style="color:red">未知</span>';
	}
	
}

function versionDbClick(owIndex, rowData){
	$("#toVersionId").val(rowData.version);
	$("#filePathId").val(rowData.filePath);
	$("#markId").val(rowData.mark);
}

function versionOnLoadSuccess(){
	$('#upgradett').datagrid('unselectAll');
}


function dataOnLoadSuccess(){
	$('#tt').datagrid('unselectAll');
}

function upgradePack(){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==undefined||recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var bizVer;
	for(var i=0;i<recordArr.length;i++){
		if(bizVer==undefined){
			bizVer=recordArr[i].bizVer;
		}
		if(bizVer!=recordArr[i].bizVer){
			$.messager.alert("提示", "请选择版本号相同的记录");
			return;
		}
		if(recordArr[i].taskStatus==1||recordArr[i].taskStatus==2){
			$.messager.alert("提示", "所选模拟器已经分配了任务，请重新选择");
			return;
		}
	}
	
	if(bizVer==undefined){
		$.messager.alert("提示", "获取不到版本号");
		return 
	}
	
	$('#upgradeWin').window('open');
	$('#upgradett').datagrid({
		url:'/daemonapk/configList?json&isDelete=0&version=' + bizVer
	});
	$('#upgradeForm').form('clear');
	$('#force').combobox('setValue', 0);
	$("#fromVersionId").val(bizVer);
}

function upgradesubmit(){
	var from=$("#fromVersionId").val();
	var to=$("#toVersionId").val();
	if(from==''||from==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(to==''||to==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(from==to){
		$.messager.alert("提示", "版本号不能相同");
		return 
	}
	
	var url=$("#filePathId").val();
	var md5=$("#markId").val();
	var force=$('#force').combobox('getValue');
	
	var taskJson={
	    fromVersion:from,
	    toVersion:to,
	    url:url,
	    md5:md5,
	    force:force
	}
	
	publishTask(JSON.stringify(taskJson));
	
}

function publishTask(taskContent){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var uuidArr=new Array();
	for(var i=0;i<recordArr.length;i++){
		uuidArr[i]=recordArr[i].uuid;
	}
	
	$.messager.confirm('提示', '你确定要发布任务吗', function(opt){
		if (opt){
			$.ajax({
				url: '/simuatorStatusInfo/upgradeTask',
				type: 'POST',
				data : {
		            uuids:uuidArr.join(","),
		            taskContent:taskContent,
		            taskType:"upgrade"
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "发布任务成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					    $('#upgradeWin').window('close');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
	
}

function closePhone(){
	var hasPass=actionTypeValidate();
	if(!hasPass){
		return;
	}
	
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	$("#forType").val('2');
	$('#ForWin').window('open');
}

function saveFor() {
	var type = $("#forType").val();
	if (type == '2') {
		phoneTask(type, "close");		
	} else if (type == '1') {
		phoneTask(type, "restart");
	} else if (type == '3') {
		phoneTask(type, "openFlashlight");
	} else {
		$.messager.alert("提示", "系统错误，请重试");
	}
	
	
}


function restartPhone(){
	var hasPass=actionTypeValidate();
	if(!hasPass){
		return;
	}
	
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	$("#forType").val('1');
	$('#ForWin').window('open');
}

function openFlashlight(){
	var hasPass=actionTypeValidate();
	if(!hasPass){
		return;
	}
	
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	$("#forType").val('3');
	$('#ForWin').window('open');
}

function phoneTask(actionType, taskType) {
	var recordArr =$('#tt').datagrid('getChecked');
	var phoneForce = $('#phoneForce').combobox('getValue'); 
	var uuidArr=new Array();
	for(var i=0;i<recordArr.length;i++){
		uuidArr[i]=recordArr[i].uuid;
	}
	
	$.ajax({
		url: '/simuatorStatusInfo/phoneTask',
		type: 'POST',
		data : {
            uuids:uuidArr.join(","),
            actionType:actionType,
            taskType:taskType,
            force:phoneForce
		},
		success : function(data) {
			if (data.return_code == '1') {
				$.messager.alert(market.content.title, "发布任务成功");
				$("#tt").datagrid('reload');
				$('#tt').datagrid('unselectAll');
			    $('#ForWin').window('close');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		error : function(e) {
			$.messager.alert("未知错误");
		}
	})
}


function actionTypeValidate(){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return false;
	}
	
	for(var i=0;i<recordArr.length;i++){
		if(recordArr[i].taskStatus==1||recordArr[i].taskStatus==2){
			$.messager.alert("提示", "所选模拟器已经分配了任务，请重新选择");
			return false;
		}
	}
	return true
}

function cancelFor(){
	$('#ForWin').window('close');
}

function searchJG(){
	var rows = $("#tt").datagrid("getSelections");
    if (rows&&rows.length>1){
    	$.messager.alert("提示", "请先选择一条记录");
		return;
    }
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	var clientIp = record.clientIp;
	if(record.clientIp==null||record.clientIp==''){
		$.messager.alert("提示", "信息错误");
		return;
	}
	$.ajax({
		url: '/simuatorStatusInfo/findCabinet',
		type: 'POST',
		data : {
			clientIp : clientIp
		},
		success : function(data) {
			if (data.return_code == '1') {
				$.messager.alert("提示", "机柜名称为：" + data.data);	
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		error : function(e) {
			$.messager.alert("未知错误");
		}
	})
}

function clearStatus() {
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return false;
	}
	var uuidArr=new Array();
	for(var i=0;i<recordArr.length;i++){
		uuidArr[i]=recordArr[i].uuid;
	}
	$.ajax({
		url: '/simuatorStatusInfo/clearStatus',
		type: 'POST',
		data : {
            uuids:uuidArr.join(",")
		},
		success : function(data) {
			if (data.return_code == '1') {
				$.messager.alert(market.content.title, "状态清理成功");
				$("#tt").datagrid('reload');
				$('#tt').datagrid('unselectAll');
			    $('#ForWin').window('close');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		error : function(e) {
			$.messager.alert("未知错误");
		}
	})
}

function syncStatus(){
	$.messager.confirm('提示', '你确定要同步状态吗', function(opt){
		if (opt){
			$.ajax({
				url: '/simuatorStatusInfo/syncStatus',
				type: 'POST',
				data : {
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "同步状态成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
}

function clearOffline(){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	$.messager.confirm('提示', '你确定要删除离线手机吗', function(opt){
		if (opt){
			var uuidArr=new Array();
			for(var i=0;i<recordArr.length;i++){
				if(recordArr[i].status=='2'){
					uuidArr[i]=recordArr[i].uuid;
				}
			}
			
			$.ajax({
				url: '/simuatorStatusInfo/clearOffline',
				type: 'POST',
				data : {
					uuids:uuidArr.join(",")
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "清理成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
}

function clearOfflineForVer(){
	var bizVer=$("#bizVerId").val();
	if(bizVer==null ||bizVer==''||bizVer==undefined){
		$.messager.alert("提示", "请先填写业务版本号");
		return;
	}
	$.messager.confirm('提示', '你确定要删除离线手机吗', function(opt){
		if (opt){
			$.ajax({
				url: '/simuatorStatusInfo/clearOfflineForVer',
				type: 'POST',
				data : {
					bizVer:bizVer
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "清理成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
}

function restartBusy(){
	$('#checkBusyWin').form('clear');
	$('#checkBusyWin').window('open');
}

function saveCheckBusy(){
	var busyTime=$("#busyTimeId").val();
	if(busyTime==undefined||busyTime==null||busyTime==''){
		$.messager.alert("提示", "请先填写时间");
		return;
	}
	
	$.messager.confirm('提示', '你确定要重启一直忙碌手机吗', function(opt){
		if (opt){
			$.ajax({
				url: '/simuatorStatusInfo/checkBusy',
				type: 'POST',
				data : {
					busyTime:busyTime
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "操作成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
						$('#checkBusyWin').window('close');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
}

function cancelCheckBusy(){
	$('#checkBusyWin').window('close');
}

//升级心跳包
function upgradeHeartApk(){
	var recordArr =$('#tt').datagrid('getChecked');
	console.info(recordArr);
	if(recordArr==undefined||recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}

	var heartVersion;
	for(var i=0;i<recordArr.length;i++){
		if(heartVersion==undefined){
			heartVersion=recordArr[i].heartVer;
		}
		if(heartVersion!=recordArr[i].heartVer){
			$.messager.alert("提示", "请选择版本号相同的记录");
			return;
		}
		
		if(recordArr[i].taskStatus==1||recordArr[i].taskStatus==2){
			$.messager.alert("提示", "所选模拟器已经分配了任务，请重新选择");
			return;
		}
	}

	if(heartVersion==undefined){
		$.messager.alert("提示", "获取不到版本号");
		return 
	}

	$('#upgradeHeartWin').window('open');
	$('#upgradehearttt').datagrid({
		url:'/heartbeat/listJson?isDelete=0&version=' + heartVersion
	});
	$('#upgradeHeartForm').form('clear');
	$('#heartForce').combobox('setValue', 0);
	$("#heartfromVersionId").val(heartVersion);
}

function heartDbClick(owIndex, rowData){
	$("#hearttoVersionId").val(rowData.version);
	$("#heartfilePathId").val(rowData.filePath);
	$("#heartmd5Id").val(rowData.mark);
	//$("#heartbucketNameId").val(rowData.bucketName);
}

function heartOnLoadSuccess(){
	$('#upgradehearttt').datagrid('unselectAll');
}


//确定升级心跳包
function upgradeHeartsubmit(){
	var from=$("#heartfromVersionId").val();
	var to=$("#hearttoVersionId").val();
	if(from==''||from==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(to==''||to==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(from==to){
		$.messager.alert("提示", "版本号不能相同");
		return 
	}
	
	var url=$("#heartfilePathId").val();
	var md5=$("#heartmd5Id").val();
	var force=$('#heartForce').combobox('getValue');
	
	var taskJson={
	    fromVersion:from,
	    toVersion:to,
	    url:url,
	    md5:md5,
	    force:force
	}
	
	heartPublishTask(JSON.stringify(taskJson));
	
}

function heartPublishTask(taskContent){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var uuidArr=new Array();
	for(var i=0;i<recordArr.length;i++){
		uuidArr[i]=recordArr[i].uuid;
	}
	
	$.messager.confirm('提示', '你确定要发布任务吗', function(opt){
		if (opt){
			$.ajax({
				url: '/simuatorStatusInfo/upgradeHeartTask',
				type: 'POST',
				data : {
		            uuids:uuidArr.join(","),
		            taskContent:taskContent,
		            taskType:"upgradeHert"
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "发布任务成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					    $('#upgradeHeartWin').window('close');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
	
}


//升级系统包
function upgradeSystemApk(){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==undefined||recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}

	var systemVersion;
	var model;
	for(var i=0;i<recordArr.length;i++){
		if(systemVersion==undefined){
			systemVersion=recordArr[i].sysVer;
		}
		if(systemVersion!=recordArr[i].sysVer){
			$.messager.alert("提示", "请选择版本号相同的记录");
			return;
		}
		
		if(model==undefined){
			model=recordArr[i].model;
		}
		if(model!=recordArr[i].model){
			$.messager.alert("提示", "请选择手机类型相同的记录");
			return;
		}
		if(recordArr[i].taskStatus==1||recordArr[i].taskStatus==2){
			$.messager.alert("提示", "所选模拟器已经分配了任务，请重新选择");
			return;
		}
	}

	if(systemVersion==undefined){
		$.messager.alert("提示", "获取不到版本号");
		return 
	}
    
    if(model==undefined){
		$.messager.alert("提示", "获取不到手机型号");
		return 
	}
	$('#upgradeSysApkWin').window('open');
	$('#upgradeSysApk').datagrid({
		url:'/systemapk/configList?json&isDelete=0&type=' + model
	});
	$('#upgradeSysApkForm').form('clear');
	$('#sysApkForce').combobox('setValue', 0);
	$("#sysApkfromVersionId").val(systemVersion);
	$("#sysApkModel").val(model);
}

function sysApkDbClick(owIndex, rowData){
	console.info(rowData);
	$("#sysApktoVersionId").val(rowData.version);
	$("#sysApkfilePathId").val(rowData.filePath);
	$("#sysApkmd5Id").val(rowData.mark);
	//$("#heartbucketNameId").val(rowData.bucketName);
}


function sysApkOnLoadSuccess(){
	$('#upgradeSysApk').datagrid('unselectAll');
}


//确定升级系统包
function upgradeSystemApksubmit(){
	var from=$("#sysApkfromVersionId").val();
	var to=$("#sysApktoVersionId").val();
	if(from==''||from==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(to==''||to==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(from==to){
		$.messager.alert("提示", "版本号不能相同");
		return 
	}
	
	var url=$("#sysApkfilePathId").val();
	var md5=$("#sysApkmd5Id").val();
	if (url == '') {
		$.messager.alert("提示", "url不能为空");
		return 
	}
	if (md5 == '') {
		$.messager.alert("提示", "md5不能为空");
		return 
	}
	var force=$('#sysApkForce').combobox('getValue');
	var doTime = $('#doTime').combobox('getValue');
	var doCount = $('#doCount').val();
	var doTranslate = $('#doTranslate').val();
	var model = $("#sysApkModel").val();
	
	var taskJson={
	    fromVersion:from,
	    toVersion:to,
	    url:url,
	    md5:md5,
	    force:force,
	    doTime : doTime,
	    doCount:doCount,
	    doTranslate:doTranslate,
	    model:model
	}
	
	sysApkPublishTask(JSON.stringify(taskJson));
	
}

function sysApkPublishTask(taskContent){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var uuidArr=new Array();
	for(var i=0;i<recordArr.length;i++){
		uuidArr[i]=recordArr[i].uuid;
	}
	
	$.messager.confirm('提示', '你确定要发布任务吗', function(opt){
		if (opt){
			$.ajax({
				url: '/simuatorStatusInfo/upgradeSysApkTask',
				type: 'POST',
				data : {
		            uuids:uuidArr.join(","),
		            taskContent:taskContent,
		            taskType:"upgradeSysApk"
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "发布任务成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					    $('#upgradeSysApkWin').window('close');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
}

//根据手机类型升级
function upgradeSystemApkByModel() {
    $('#upgradeSysApkFormByType').form('clear');
    $('#modelByType').combobox('setValue', -1);
    $('#sysApkForceByType').combobox('setValue', 0);
	$('#upgradeSysApkByTypeWin').window('open');
}

function sysApkDbClickType(owIndex, rowData){
	$("#sysApktoVersionByType").val(rowData.version);
	$("#sysApkfilePathIdByType").val(rowData.filePath);
	$("#sysApkmd5IdByType").val(rowData.mark);
}
//确定升级
function upgradeSystemApksubmitByType() {
	var to=$("#sysApktoVersionByType").val();
	if(to==''||to==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	
	var url=$("#sysApkfilePathIdByType").val();
	var md5=$("#sysApkmd5IdByType").val();
	if (url == '') {
		$.messager.alert("提示", "url不能为空");
		return 
	}
	if (md5 == '') {
		$.messager.alert("提示", "md5不能为空");
		return 
	}
	var force=$('#sysApkForceByType').combobox('getValue');
	var doTime = $('#doTimeByType').combobox('getValue');
	var doCount = $('#doCountByType').val();
	var doTranslate = $('#doTranslateByType').val();
	var model = $("#modelByType").combobox('getValue');
	
	var taskJson={
	    toVersion:to,
	    url:url,
	    md5:md5,
	    force:force,
	    doTime : doTime,
	    doCount:doCount,
	    doTranslate:doTranslate,
	    model:model
	}
	sysApkPublishTaskByType(JSON.stringify(taskJson));
}

function sysApkPublishTaskByType(taskContent){
	$.messager.confirm('提示', '你确定要发布任务吗', function(opt){
		if (opt){
			$.ajax({
				url: '/simuatorStatusInfo/upgradeSysApkTaskByType',
				type: 'POST',
				data : {
		            taskContent:taskContent
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "发布任务成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					    $('#upgradeSysApkByTypeWin').window('close');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
}

function searchSysApkTask() {
	showMyWindow('待执行任务查询', '/simuatorStatusInfo/seachSysTask', 700, 500);
}
